/*************************************************************************
	> File Name: hzoj_254.cpp
	> Author: zhangxu 
	> Mail: walrus1983@126.com
	> Created Time: 二  9/ 3 12:50:59 2024

	按挤奶开始时间排序，将奶牛安排给可以安排的编号最小的挤奶机，当无法安排时，增加
	挤奶机数量
 ************************************************************************/

#include <cstdio>
#include <algorithm>
// #include<iostream>
using namespace std;
#define MAX_N 50000

struct MilkCow {
	int id, start, end;
} arr[MAX_N + 5];

int m_time[MAX_N + 5], ans[MAX_N + 5];
int cnt = 0;

int main() {
	int n;
	scanf("%d", &n);
	for(int i = 0, x, y; i < n; i++) {
		scanf("%d%d", &arr[i].start, &arr[i].end);
		arr[i].id = i;
	}
	sort(arr, arr + n, [&] (const MilkCow &a, const MilkCow &b) -> bool {
		if(a.start != b.start) return a.start < b.start;
		return a.id < b.id;
	});
	for(int i = 0; i < n; i++) {
		int pos = -1;
		for(int j = 0; j < cnt; j++) {
			if(m_time[j] < arr[i].start) {
				pos = j;
				break ;
			}
		}
		if(pos == -1) pos = (cnt++);
		m_time[pos] = arr[i].end;
		ans[arr[i].id] = pos + 1;
	}
	printf("%d\n", cnt);
	for(int i = 0; i < n; i++) {
		printf("%d\n", ans[i]);
	}
	return 0;
}